//
//  SHLoginView.m
//  SimpleHappy
//
//  Created by 张超 on 15/11/24.
//  Copyright © 2015年 RockyFung. All rights reserved.
//

#import "SHLoginView.h"
#import "Define.h"
#import "SHRegistView.h"
#import "SHCoreData.h"
#import <BmobSDK/Bmob.h>

@interface SHLoginView ()

@end

@implementation SHLoginView

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 用户名、密码输入框及输入框下划线
    self.userId = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth / 1.2, KScreenHeight / 22.23)];
    self.userId.center = CGPointMake(KScreenWidth / 2, KScreenHeight / 5);
    self.userId.placeholder = @"输入帐号";
    [self.view addSubview:self.userId];
    
    self.pwd = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth / 1.2, KScreenHeight / 22.23)];
    self.pwd.center = CGPointMake(KScreenWidth / 2, self.userId.frame.origin.y + KScreenHeight / 7.41);

    self.pwd.placeholder = @"输入6-16位登录密码";
    self.pwd.secureTextEntry = YES;

    self.pwd.placeholder = @"输入登录密码";

    [self.view addSubview:self.pwd];
    
    UIView *userIdView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.userId.bounds.size.width, 1)];
    userIdView.backgroundColor = [UIColor blackColor];
    userIdView.center = CGPointMake(KScreenWidth / 2, self.userId.frame.origin.y + self.userId.bounds.size.height);
    [self.view addSubview:userIdView];
    
    UIView *pwdView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.pwd.bounds.size.width, 1)];
    pwdView.backgroundColor = [UIColor blackColor];
    pwdView.center = CGPointMake(KScreenWidth / 2, self.pwd.frame.origin.y + self.pwd.bounds.size.height);
    [self.view addSubview:pwdView];
    
    // 登录
    self.loginBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth / 3, KScreenWidth / 8)];
    self.loginBtn.center = CGPointMake(KScreenWidth / 2, KScreenHeight / 2.3);
    [self.loginBtn setTitle:@"登 录" forState:UIControlStateNormal];
    [self.loginBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.loginBtn addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.loginBtn];

}

- (void)loginAction:(UIButton *)btn
{
    
    [BmobUser loginWithUsernameInBackground:self.userId.text password:self.pwd.text block:^(BmobUser *user, NSError *error) {
        if (error) {
////////////// 登陆失败
            NSLog(@"%@",error.userInfo);
            // 警告框
            UIAlertController *alertController =[UIAlertController alertControllerWithTitle:@"登陆失败！" message:[NSString stringWithFormat:@"原因:%@",error.userInfo] preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"重新登陆" style:UIAlertActionStyleCancel handler:nil];
            UIAlertAction *goToRegist = [UIAlertAction actionWithTitle:@"去注册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                // 登陆失败跳转到注册页面
                [super registAction:[super registBtn]];
            }];
            [alertController addAction:goToRegist];
            [alertController addAction:cancelAction];
            [self presentViewController:alertController animated:YES completion:nil];

        }else{
/////////////// 登陆成功
            NSLog(@"%@登陆成功",user);
            // 警告框
            UIAlertController *alertController =[UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"%@,登陆成功！",user.username] message:nil preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                [self dismissViewControllerAnimated:YES completion:nil];
            }];
            [alertController addAction:cancelAction];
            [self presentViewController:alertController animated:YES completion:nil];
            
            // 回调函数
            if (self.block != nil) {
                self.block(user.username);
            }
            
        }
    }];
    
}

// 回收键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

@end
